/*
 * Created on Feb 17, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

import com.scalar.ScalarException;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.Address;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.OpenPurchaseOrderForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.operations.vendor.Vendor;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;


/**
 * @author Ravikanth
**/
public class OpenPurchaseOrderConfirmCommand extends ActionCommand
{
	public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)	throws ScalarException
	{
		HashMap errors			= ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String actionForward 	= "viewOpenPurchaseOrder";
		String action   		= request.getParameter( REQUEST_ACTION );
		HttpSession mSession 	= request.getSession();
		OpenPurchaseOrderForm purchaseOrderForm = ( OpenPurchaseOrderForm ) form;

		try
		{
			if( ACTION_CONFIRM.equalsIgnoreCase( action ) )
			{
				actionForward = "viewOpenPurchaseOrder";
                if( purchaseOrderForm.getPurchaseOrder_id() == -1 )
				{
					setPurchaseOrderShippingAddress( purchaseOrderForm );
                }
			}
		}
		catch ( Exception e )
		{
			Logger.error( "", e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, "" );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
			actionForward = "search";
		}
		        
		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
	}

	/**
	 * @param purchaseOrderForm
	 */
	private void setPurchaseOrderShippingAddress(OpenPurchaseOrderForm purchaseOrderForm) throws ScalarException
	{

		Address address 		= null;
		String contactPersonAddress_Id 	= purchaseOrderForm.getContactPerson();
        String vendorSalesAddress_Id 	= purchaseOrderForm.getVendorAddressId();
        if( vendorSalesAddress_Id != null   && !vendorSalesAddress_Id.equalsIgnoreCase("-1") )
        {
            ArrayList salesAddressList 	= PurchaseOrderDAO.loadVendorSalesAddresses( purchaseOrderForm.getVendor_id() );
            for( int i = 0; i < salesAddressList.size(); i++ )
			{
				address = (Address)salesAddressList.get( i );
				if( ( address.getContactPersonAddressId()+"" ).equalsIgnoreCase( vendorSalesAddress_Id ) )
				{
					purchaseOrderForm.setShippingAddress( address );
				}
            }
        }
        else if( contactPersonAddress_Id != null   && !contactPersonAddress_Id.equalsIgnoreCase("-1") )
        {
            ArrayList addressList 	= PurchaseOrderDAO.loadVendorShippingAddresses( purchaseOrderForm.getVendor_id() );
            for( int i = 0; i < addressList.size(); i++ )
			{
				address = (Address)addressList.get( i );
				if( ( address.getContactPersonAddressId()+"" ).equalsIgnoreCase( contactPersonAddress_Id ) )
				{
					purchaseOrderForm.setShippingAddress( address );
				}
            }
        }

    }
}
